﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class DepartmentInfoController : Controller
    {
        private IDepartmentInfoBLL _departmentInfoBLL;
        public DepartmentInfoController(
            IDepartmentInfoBLL departmentInfoBLL
            )
        {
            _departmentInfoBLL = departmentInfoBLL;
        }

        public IActionResult CreateDepartmentInfoListView()
        {
            return View();
        }


        // GET: DepartmentInfo
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult UpdateDepartmentInfoView()
        {
            return View();
        }



        #region 返回父级部门和领导信息下拉列表数据(GetSelectOptions)

        [HttpGet]
        public IActionResult GetSelectOptions()
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用BLL方法
            var data = _departmentInfoBLL.GetSelectOptions();

            //构建返回结果进行返回
            result.Code = 200;
            result.Msg = "获取成功";
            result.IsSuccess = true;
            result.Data = data;
            return Json(result);

        }

        #endregion



        #region 创建部门的接口(CreateDepartmentInfo)

        [HttpPost]
        public IActionResult CreateDepartmentInfo([FromForm] DepartmentInfo departmentInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _departmentInfoBLL.CreateDepartmentInfo(departmentInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);

        }

        #endregion


        #region 获取列表的接口(GetDepartmentInfos)

        [HttpGet]
        public IActionResult GetDepartmentInfos(int page, int limit, string departmentName)
        {
            //声明返回对象
            ReturnResult result = new ReturnResult();
            //声明一个总数的变量
            int count;
            //调用BLL的获取用户方法
            var list = _departmentInfoBLL.GetDepartmentInfos(page, limit, departmentName, out count);
            //返回值
            result.Code = 0;
            result.IsSuccess = true;
            result.Msg = "获取成功！";
            result.Count = count;
            result.Data = list;


            //返回结果
            return Json(result);

        }
        #endregion


        #region 根据id获取部门(GetDepartmentInfoById)
        /// <summary>
        /// 根据id获取部门详情
        /// </summary>
        /// <param name="id">要编辑的部门id</param>
        /// <returns></returns>

        [HttpGet]
        public IActionResult GetDepartmentInfoById(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //判断id是否为空
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空";
                return Json(result);
            }
            //根据id查询部门信息，并判断是否为空
            DepartmentInfo departmentInfo = _departmentInfoBLL.GetDepartmentInfoById(id);

            if (departmentInfo == null)
            {
                result.Msg = "未获取到部门信息";
            }
            else
            {
                //调用获取父级下拉列表和领导列表下拉列表的接口
                var selectOption = _departmentInfoBLL.GetSelectOptions();

                result.Code = 200;
                result.Msg = "查询成功";
                result.Data = new
                {
                    departmentInfo,//返回部门信息
                    selectOption//下拉类别的信息
                };
            }
            return Json(result);
        }

        #endregion



        #region 修改部门的接口(UpdateDepartmentInfo)
        /// <summary>
        /// 修改部门的接口
        /// </summary>
        /// <param name="department">修改部门的信息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateDepartmentInfo([FromForm] DepartmentInfo department)
        {

            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _departmentInfoBLL.UpdateDepartmentInfo(department, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion


        #region 用户软删除的接口(DeleteUserInfo)
        /// <summary>
        /// 用户删除的接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteDepartmentInfo(string id)
        {
            //实例化返回结果
            ReturnResult result = new ReturnResult();

            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _departmentInfoBLL.DeleteDepartmentInfo(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除用户成功!";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion


        #region 用户软批量删除的接口(DeleteUserInfo)
        [HttpPost]
        public IActionResult DeleteDepartmentInfos(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //判断非空

            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的用户！";
                return Json(result);
            }
            //调用批量删除的方法
            bool isOk = _departmentInfoBLL.DeleteDepartmentInfos(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败";
            }
            return Json(result);//返回结果

        }

        #endregion


    }
}
